package edu.whu.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import edu.whu.demo.dao.Commdity_SuppliersDao;
import edu.whu.demo.domain.Commdity_SuppliersDTO;
import edu.whu.demo.domain.Commodity;
import edu.whu.demo.dao.CommodityDao;
import edu.whu.demo.service.ICommodityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zxj
 * @since 2023-10-18
 */
@Service
public class CommodityServiceImpl extends ServiceImpl<CommodityDao, Commodity> implements ICommodityService {
    @Autowired
    CommodityDao commodityDao;

    @Autowired
    private Commdity_SuppliersDao commditySuppliersDao;

    @Cacheable(cacheNames = "commodity",key = "#commodity",condition = "#commodity!=null")
    public Commodity addCommdity(Commodity commodity){
        commodityDao.insert(commodity);
        return commodity;
    }

    @CacheEvict(cacheNames = "commodity",key = "#commodity")
    public void delCommdityById(int id){
        commodityDao.deleteById(id);
    }

    //根据id查询
    public Commodity findCommdityById(int id){
        return commodityDao.selectById(id);
    }

    //查询所有
    public List<Commodity> findAll(){
        return commodityDao.selectList(new QueryWrapper<Commodity>());
    }

    //条件查询
    @Override
    public List<Commodity> list(Wrapper<Commodity> queryWrapper) {
        return super.list(queryWrapper);
    }

    //多表查询
    public Commdity_SuppliersDTO getCommdityWithSuppliers(int id){
        return commditySuppliersDao.selectCommodityWithSuppliers(id);
    }


    public void updateCommdityById(Commodity commodity){
        commodityDao.updateById(commodity);
    }


}
